/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.manager;

import com.ez2find.model.Area;
import com.ez2find.model.Hous;
import com.ez2find.model.CategoriaInmobiliario;
import com.ez2find.model.RegisterUser;
import com.ez2find.model.Site;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Owner
 */
@Stateless
public class HousManagerBean implements HousManagerLocal {
    
    @PersistenceContext
    private EntityManager em;
    
    public void create(Hous hous)throws Exception {
        em.persist(hous);
    }
 
    public void update(Hous hous)throws Exception {
        em.merge(hous);
    }
    
    public Hous merge(Hous hous)throws Exception {
        return em.merge(hous);
    }
    
    public void delete(Hous hous)throws Exception {
        hous=em.merge(hous);
        em.remove(hous);
    }
    
    public Hous find(long id){
        return em.find(Hous.class,id);
    }

    public List<Hous> listAll(){
        Query query=em.createNamedQuery("listAllHousing");
        List<Hous> result=query.getResultList();
        return result;
    }
    
    public long lastCode(){
        try{
            Query query=em.createNamedQuery("lastHousCode");
            return (Long) query.getSingleResult();
        }catch(Exception ex){
            return 0;
        }
    }
    
    public List<Hous> listHousingSite(Site site, boolean housC, CategoriaInmobiliario housCategorie, String consult){
        Query query=em.createQuery("SELECT h FROM Hous h WHERE h.site=:site AND h.status='active'"+consult+" ORDER BY h.publishDate DESC, h.code DESC");
        query.setParameter("site", site);
        if(housC)
            query.setParameter("housCategorie", housCategorie);
        List<Hous> result=query.getResultList();
        return result;
    }
    
    public List<Hous> listHousingArea(Area area, boolean housC, CategoriaInmobiliario housCategorie, String consult){
        Query query=em.createQuery("SELECT h FROM Hous h WHERE h.area=:area AND h.status='active'"+consult+" ORDER BY h.publishDate DESC, h.code DESC");
        query.setParameter("area", area);
        if(housC)
            query.setParameter("housCategorie", housCategorie);
        List<Hous> result=query.getResultList();
        return result;
    }
    
}
